#include "station.h"
#include "hj212.h"
#include "devicelist.h"
#include "globalobject.h"
#include <QProcess>
#include <QDebug>


    Station::Station()
    {

        netbox.setCallbackFunc(recvHostCallBack);
        serialbox.setCallbackFunc(recvDeviceCallBack);


    //	device_list = new DeviceModle();
    }


    Station::~Station()
    {

    }


void Station::showRequest(int Index)//, const QString &s)
{
        qDebug()<<"Station:"<<Index<<"\r\n";
}

//void Station::TimeOutUpdateSlot()
//{
//    //qDebug()<<"timeout";
//    //clickHostItem(0);
//}
void Station::clickDeviceItem(int index)
{
    //deviceSendAuto(index);
}
void Station::clickHostItem(int index)
{
    HostSendAuto(1013,index);//时间校准请求
    //HostSendAuto(2081,index);//上传数采仪开机时间

}
//void qmlHostSendAuto(int CN,int hostIndex)
//{
//    Station::HostSendAuto(CN,hostIndex);
//}
void Station::recvDeviceCallBack(int devIndex,QString devName,QByteArray res,int error)
{
    //qDebug()<<dev<<"--"<<res<<"--\r\n";
   if(0==error)
        processRecvDeviceBackMsg(devIndex,devName,res);
   else
        processRecvDeviceBackError(devIndex,QString::fromLatin1(res),error);
}

void Station::recvHostCallBack(int hostIndex,QString hostName,QString res,int error)
{
   //qDebug()<<hostIndex<<"--"<<res<<"--\r\n";
   if(0==error)
        processRecvHostBackMsg(hostIndex,hostName,res);
   else
       hostlist.changeDataString(hostIndex,res,error);
}

void Station::recvTimerCallBack(qint64 UtcSec)
{

  processRecvTimerBackMsg(UtcSec);
}

void Station::deviceSendAuto(int deviceIndex,int funCode)
{
    //取出设备(端口，设备协议，工作任务)
    Device device=devicelist.getdata()[deviceIndex];

    //打开设备端口，根据工作任务调用协议数据生成方法指令数据，从端口发送指令
    serialbox.writeDataToSerial(deviceIndex,device.name,device.portName,device.portBaudRate,device.portDataBits,device.portParity,device.portStopBits,1000,device.cmdProcessFun(&device,funCode));
}
void Station::processRecvTimerBackMsg(qint64 UtcSec)
{


    station.currTimeSec=UtcSec;
    station.refreshSystemTime();
    //初次启动初始化相关延时，避免上电时触发所有上报
    if(0==station.startupTimeSec)
    {
        station.startupTimeSec=UtcSec;
        station.reSendTimeSec=station.currTimeSec+13;
        devicelist.deviceReadNextSec=station.currTimeSec+10;
        station.rtdTimeSec = station.currTimeSec + station.getRtdIntervalInt();
        station.minTimeSec = station.currTimeSec + (60*station.getMinIntervalInt());
        station.hourTimeSec = ((station.currTimeSec/3600)+1)*3600;
        //每天0点上报日数据,系统时间为UTC时间,因此整点计算时先算出UTC往后偏移8小时后的当天零点(东八区的当天零点),再次该零点(东八区零点)住后延一天后，再前偏移8小时得到东八区下一个零点的UTC时间
        station.dayTimeSec = (((station.currTimeSec+(3600*8))/(3600*24))*(3600*24))+(3600*(24-8));

    }


    //数据采集
    if(station.currTimeSec>=devicelist.deviceReadNextSec)
    {

        if(devicelist.getdataSize()>0)
        {
            deviceSendAuto(devicelist.deviceReadNextIndex,devicelist.getCurrDeviceCurrFuncode());
            devicelist.deviceOrFunIndexNext();
        }

        //设备轮询采集间隔
        devicelist.deviceReadNextSec=station.currTimeSec+10;

    }


    //数据上报
    if(station.currTimeSec>=station.rtdTimeSec)
    {
        //实时间隔上报
        station.rtdTimeSec = station.currTimeSec + station.getRtdIntervalInt();
        if(station.getRealtimeReport()) processRtdSendToHost(UtcSec); //污染物实时数据上报
        //if(station.getDeviceStateView()) //治理设备状态实时上报
    }
    else if(station.currTimeSec>=station.minTimeSec)
    {
        //分钟间隔上报
        station.minTimeSec = station.currTimeSec + (60*station.getMinIntervalInt());
        processMinSendToHost(UtcSec);
    }
    else if(station.currTimeSec>=station.hourTimeSec)
    {
        //每个整点上报时数据
        station.hourTimeSec = ((station.currTimeSec/3600)+1)*3600;
        processHourSendToHost(UtcSec);
    }
    else if(station.currTimeSec>=station.dayTimeSec)
    {
        //每天0点上报日数据
        station.dayTimeSec = (((station.currTimeSec+(3600*8))/(3600*24))*(3600*24))+(3600*(24-8));//系统时间为UTC时间,因此整点计算时先算出UTC往后偏移8小时后的当天零点(东八区的当天零点),再次该零点(东八区零点)住后延一天后，再前偏移8小时得到东八区下一个零点的UTC时间
        processDaySendToHost(UtcSec);
    }
    else if(station.currTimeSec>=station.reSendTimeSec)
    {
        //时报和日报失败重发在此处理触发上传每12秒检查一次,时间设置过小,会造成命令缓冲区堆积,确保有足够时间发送完数据或有时间超时清空
        station.reSendTimeSec=station.currTimeSec+12;
        processNotSendOkToHost(UtcSec);
    }


}


void Station::processRtdSendToHost(qint64 UtcSec)
{
    qDebug()<<"-processRtdSendToHost-"<<UtcSec<<"\r\n";

    HostSendAuto(2011);
}
void Station::processMinSendToHost(qint64 UtcSec)
{
    qDebug()<<"-processMinSendToHost-"<<UtcSec<<"\r\n";
    qint64 queryTimeSec=UtcSec-(60*station.getMinIntervalInt());
    for(int i=0;i<codelist.getdataSize();i++)
    {
        QVariantList datalist;
        Code *code=codelist.getpdataByIndex(i);
        datalist.clear();
        datalist=sqldate->Query("select MAX(Rtd) AS Max,MIN(Rtd) AS Min,AVG(Rtd) AS Avg FROM hj212data_code where codeId="+code->id+" AND DataTimeSec>="+QString::number(queryTimeSec));
        if(datalist.count()>0)
        {
            code->cpData["M-Min"]=datalist.at(0).value<QVariantMap>()["Min"].toString();
            code->cpData["M-Avg"]=HJ212::formatNumber(code->dataFormat,datalist.at(0).value<QVariantMap>()["Avg"].toDouble());
            code->cpData["M-Max"]=datalist.at(0).value<QVariantMap>()["Max"].toString();
            code->cpData["M-Flag"]=dataFlagNormal;
        }
    }
    HostSendAuto(2051);

}



void Station::processHourSendToHost(qint64 UtcSec)
{
    qDebug()<<"-processHourSendToHost-"<<UtcSec<<"\r\n";
    //删除5天前的实时数据
    sqldate->EXEC("delete from hj212data_device where DataTimeSec<"+QString::number(UtcSec-(3600*24*5)));
    //删除15天前的时报日报数据
    sqldate->EXEC("delete from hj212data_server where DataTimeSec<"+QString::number(UtcSec-(3600*24*15)));
    //生成小时数据
    qint64 queryTimeSec=UtcSec-(3600*24);
    for(int i=0;i<codelist.getdataSize();i++)
    {
        QVariantList datalist;
        Code *code=codelist.getpdataByIndex(i);
        datalist.clear();
        datalist=sqldate->Query("select MAX(Rtd) AS Max,MIN(Rtd) AS Min,AVG(Rtd) AS Avg FROM hj212data_code where codeId="+code->id+" AND DataTimeSec>="+QString::number(queryTimeSec));
        if(datalist.count()>0)
        {
            code->cpData["H-Min"]=datalist.at(0).value<QVariantMap>()["Min"].toString();
            code->cpData["H-Avg"]=HJ212::formatNumber(code->dataFormat,datalist.at(0).value<QVariantMap>()["Avg"].toDouble());
            code->cpData["H-Max"]=datalist.at(0).value<QVariantMap>()["Max"].toString();
            code->cpData["H-Flag"]=dataFlagNormal;
        }
    }
    HostSendAuto(2061);
}

void Station::processDaySendToHost(qint64 UtcSec)
{
    qDebug()<<"-processDaySendToHost-"<<UtcSec<<"\r\n";
}

void Station::processNotSendOkToHost(qint64 UtcSec)
{
    qDebug()<<"-processNotSendOkToHost----"<<UtcSec<<"\r\n";
    //qint64 queryTimeSec=UtcSec-(3600*24*3);//只处理3天以内的未上报数据
    QVariantList datalist;
    datalist.clear();
    datalist=sqldate->Query("select * FROM sendtask_view where sendNotOk>0 order by sendQN asc",0,1);//限制每次最多处理的条数为1条,队列多条会出现无法接收反馈的情况

    if(datalist.size()>0)
    {

        for(int i=0;i<datalist.count();i++)
        {
            //准备数据包
            HJ212_DATA object;
            object.QN=HJ212::getQN();
            object.ST=datalist.at(i).value<QVariantMap>()["st"].toInt();
            object.CN=datalist.at(i).value<QVariantMap>()["cn"].toInt();
            object.CP17=datalist.at(i).value<QVariantMap>()["cpData"].toString();
            object.CP05=datalist.at(i).value<QVariantMap>()["cpDataOld"].toString();

            int hostIndex=hostlist.getdataIndexById(datalist.at(i).value<QVariantMap>()["serverId"].toString());
            object.MN=hostlist.getMNByIndex(hostIndex);
            object.PW=hostlist.getPWByIndex(hostIndex);
            object.Flag=HJ212::getFlag(hostlist.getFlagByIndex(hostIndex),true);

            //更新sendQN
            SetData::Update_ServerSendTasksendQN(datalist.at(i).value<QVariantMap>()["serverId"].toString(),datalist.at(i).value<QVariantMap>()["sendQN"].toString(),object.QN);

            netbox.writeDataToHost(hostIndex,hostlist.getdata()[hostIndex],1000,HJ212::ResponseData(object));//s

        }
    }
}

void Station::HostSendAuto(int CN,int hostIndex)
{

    //准备数据包
    HJ212_DATA object;
    object.QN=HJ212::getQN();
    object.ST=32;
    object.CN=CN;
//    object.PW=station.getPW();
//    object.MN=station.getMN();

    if(2011==CN)//上传/污染物ST=32/噪声声级ST=23/工况ST=52/实时数据
    {

//        object.CP17="";
//        object.CP05="";

        //生成数据
        object.CP17="DataTime="+HJ212::getDataTime();
        object.CP05="DataTime="+HJ212::getDataTime();

        for(int i=0;i<codelist.getdataSize();i++)
        {
            Code* code=codelist.getpdataByIndex(i);
            if(code->deviceIndex>=0)
            {
                object.CP17+=";"+code->code+"-Rtd="+code->cpData["Rtd"].toString();
                object.CP17+=","+code->code+"-Flag="+code->cpData["RtdFlag"].toString();
                if(!code->codeOld.isNull())
                {
                    object.CP05+=";"+code->codeOld+"-Rtd="+code->cpData["Rtd"].toString();
                    object.CP05+=","+code->codeOld+"-Flag="+code->cpData["RtdFlag"].toString();
                }
            }
        }

    }
    else if(2051==CN)//上传/污染物ST=32/噪声声级ST=23/分钟数据
    {

//        object.CP17="";
//        object.CP05="";

        //生成数据

        object.CP17="DataTime="+HJ212::getDataTime();
        object.CP05="DataTime="+HJ212::getDataTime();
        for(int i=0;i<codelist.getdataSize();i++)
        {
            Code* code=codelist.getpdataByIndex(i);

            object.CP17+=";"+code->code+"-Cou="+code->cpData["M-Cou"].toString();
            object.CP17+=","+code->code+"-Min="+code->cpData["M-Min"].toString();
            object.CP17+=","+code->code+"-Avg="+code->cpData["M-Avg"].toString();
            object.CP17+=","+code->code+"-Max="+code->cpData["M-Max"].toString();
            object.CP17+=","+code->code+"-Flag="+code->cpData["M-Flag"].toString();
            if(!code->codeOld.isNull())
            {
                object.CP05+=";"+code->codeOld+"-Cou="+code->cpData["M-Cou"].toString();
                object.CP05+=","+code->codeOld+"-Min="+code->cpData["M-Min"].toString();
                object.CP05+=","+code->codeOld+"-Avg="+code->cpData["M-Avg"].toString();
                object.CP05+=","+code->codeOld+"-Max="+code->cpData["M-Max"].toString();
                object.CP05+=","+code->codeOld+"-Flag="+code->cpData["M-Flag"].toString();
            }

        }

    }
    else if(2061==CN)//上传/污染物ST=32/噪声声级ST=23/小时数据
    {

//        object.CP17="";
//        object.CP05="";

        //生成数据
        object.CP17="DataTime="+HJ212::getDataTime();
        object.CP05="DataTime="+HJ212::getDataTime();

        for(int i=0;i<codelist.getdataSize();i++)
        {
            Code* code=codelist.getpdataByIndex(i);
            object.CP17+=";"+code->code+"-Cou="+code->cpData["H-Cou"].toString();
            object.CP17+=","+code->code+"-Min="+code->cpData["H-Min"].toString();
            object.CP17+=","+code->code+"-Avg="+code->cpData["H-Avg"].toString();
            object.CP17+=","+code->code+"-Max="+code->cpData["H-Max"].toString();
            object.CP17+=","+code->code+"-Flag="+code->cpData["H-Flag"].toString();
            if(!code->codeOld.isNull())
            {
                object.CP05+=";"+code->codeOld+"-Cou="+code->cpData["H-Cou"].toString();
                object.CP05+=","+code->codeOld+"-Min="+code->cpData["H-Min"].toString();
                object.CP05+=","+code->codeOld+"-Avg="+code->cpData["H-Avg"].toString();
                object.CP05+=","+code->codeOld+"-Max="+code->cpData["H-Max"].toString();
                object.CP05+=","+code->codeOld+"-Flag="+code->cpData["H-Flag"].toString();
            }
        }

        //发送的数据写入数据库
        QVariantMap dbdata;
        dbdata.clear();
        dbdata.insert("qn",object.QN);
        dbdata.insert("st",object.ST);
        dbdata.insert("cn",object.CN);
        dbdata.insert("cpData",object.CP17);
        dbdata.insert("cpDataOld",object.CP05);
        dbdata.insert("dataTimeSec",station.currTimeSec);
        sqldate->INSERT("hj212data_server",dbdata);

        //给每个服务器记录发送任务
        for(int i=0;i<hostlist.getdataSize();i++)
        {
        Host *host=hostlist.getpdataByIndex(i);
        dbdata.clear();
        dbdata.insert("qn",object.QN);
        dbdata.insert("sendqn",object.QN);
        dbdata.insert("serverId",host->id);
        sqldate->INSERT("hj212data_sendtask",dbdata);
        qDebug()<<"sendQN:"<<object.QN;
        }

    }
    else if(2031==CN)//上传/污染物ST=32/噪声声级ST=23/日历史数据
    {

    }
    else if(2021==CN)//上传设备运行状态
    {
        object.CP17="DataTime="+HJ212::getDataTime()+";SB1-RS=2;SB2-RS=0...";
    }
    else if(3019==CN)//上传设备唯一标识
    {
        object.CP17="DataTime="+HJ212::getDataTime()+";PolId=w010108;w010108-SN=010000A8900016F00169DC1";
    }
    else if(2041==CN)//上传设备运运时间日历史数据
    {
        object.CP17="DataTime="+HJ212::getDataTime()+";SB1-RT=1.1;SB2-RT=2.1...";
    }
    else if(2081==CN)//上传数采仪开机时间
    {
        object.CP17="DataTime="+HJ212::getDataTime()+";RestartTime="+station.getRestartTime();
    }
    else if(1013==CN)//时间校准请求
    {
        object.CP17="PolId=w010118";
    }
    else if(3020==CN)//上传现场机信息
    {
        //(日志)
        object.CP17="DataTime="+HJ212::getDataTime()+";PolId=w010108;i11001-Info=//清洗管路//";
        //object.CP17=object.CP17.toLatin1();
//        //(状态)
//        object.CP17="DataTime="+HJ212::getDataTime()+";PolId=w010108;i12001-Info=1;i12003-Info=0";
//        //(参数)
//        object.CP17="DataTime="+HJ212::getDataTime()+";PolId=w010108;i13004-Info=168.0;i13005-Info=40";
    }
    else if(3019==CN)//上传现场机信息(状态)
    {
        object.CP17="DataTime="+HJ212::getDataTime()+";PolId=w010108;i11001-Info=//清洗管路//";
    }

    if(hostIndex>=0)//发送到指定平台
    {
        //object.CP17=object.CP17.toLatin1();
        object.MN=hostlist.getMNByIndex(hostIndex);
        object.PW=hostlist.getPWByIndex(hostIndex);
        object.Flag=HJ212::getFlag(hostlist.getFlagByIndex(hostIndex),true);
        netbox.writeDataToHost(hostIndex,hostlist.getdata()[hostIndex],1000,HJ212::ResponseData(object));//s

    }
    else//发送到所有平台
    {
        for(int i=0;i<hostlist.getdata().size();i++)
        {
            object.MN=hostlist.getMNByIndex(i);
            object.PW=hostlist.getPWByIndex(i);
            object.Flag=HJ212::getFlag(hostlist.getFlagByIndex(i),true);
            netbox.writeDataToHost(i,hostlist.getdata()[i],1000,HJ212::ResponseData(object));//s
        }
    }


}



void Station::processRecvDeviceBackMsg(int index,QString dev,QByteArray resData)
{
    //QString res=QString::fromLatin1(resData);
    //取出设备(端口，设备协议，工作任务)
    Device* device=devicelist.getpdataByIndex(index);
    //根据设备协议解析数据，并返回显示字符串
    QString datares=device->dataProcessFun(device,resData);

    //更新界面数据显示字串
    QString timeStr = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    devicelist.changeDataString(index,datares+" "+timeStr);

    if(!datares.startsWith("Error:"))
    {
        for(int i=0;i<codelist.getdataSize();i++)
        {
            Code* code=codelist.getpdataByIndex(i);
            //qDebug()<<code->deviceIndex<<index;
            if(code->deviceIndex==index)
            {
                QString datastr=code->dataString+" "+timeStr;
                codelist.changeDataString(i,datastr);
                if(!code->cpData["Rtd"].isNull())
                {
                    //插入数据库
                    QVariantMap data;
                    data.clear();
                    data.insert("codeId",code->id);
                    data.insert("Rtd",code->cpData["Rtd"]);
                    data.insert("Cou",code->cpData["Cou"]);
                    data.insert("DataTimeSec",QString::number(station.currTimeSec));
                    sqldate->INSERT("hj212data_code",data);
                }
            }
        }
    }




}

void Station::processRecvDeviceBackError(int index,QString res,int error)
{
    //取出设备(端口，设备协议，工作任务)
    Device* device=devicelist.getpdataByIndex(index);

    //设置设备故障标志
    device->dataFlag=dataFlagCommError;
    devicelist.changeDataString(index,res,error);
}



void Station::processRecvHostBackMsg(int index,QString hostname,QString res)
{
    //取出设备(端口，设备协议，工作任务)
    Host host=hostlist.getdata()[index];

    //根据设备协议解析数据，并返回显示字符串

    //qDebug()<<index<<dev<<"--"<<res<<"--\r\n";
    HJ212_DATA object;
    int error=HJ212::getDataObject(res,object,host);//station.getMN(),station.getPW());
    if(0==error)
    {
        QString datares;//=res;//Flowmeter::getDataResultStr(device.protocol,device.comAddr,res);
        datares.sprintf("ST:%d,CN:%d,Flag:%d",object.ST,object.CN,object.Flag);
        if(object.CP17.size()>0) datares+=",CP:"+object.CP17;
        //更新界面数据显示字串
        QString timeStr = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
        QString datastr=datares+" "+timeStr;
        hostlist.changeDataString(index,datastr);

        //处理数据

//        object.MN=hostlist.getMNByIndex(hostIndex);
//        object.PW=hostlist.getPWByIndex(hostIndex);
        object.Flag=HJ212::getFlag(hostlist.getFlagByIndex(index));

        if(1000==object.CN)
        {
            netbox.writeDataToHost(index,host,500,HJ212::ReqReponse(object));
            HJ212::recvCN1000(object.CP17,station);
            netbox.writeDataToHost(index,host,500,HJ212::ReqResult(object));
        }
        else if(1011==object.CN)
        {
            QString cpStr;
            netbox.writeDataToHost(index,host,500,HJ212::ReqReponse(object));
            //如有污染物编码则取出污染物编码
            if(object.CP17.size()>5)
            {
                QString polId=HJ212::recvCN1011(object.CP17);
                //找出对应设备，根据协议取出设备的时间
                cpStr="polId="+polId+";SystemTime="+station.getSystemTime();
            }
            else
            {
                cpStr="SystemTime="+station.getSystemTime();
            }

            netbox.writeDataToHost(index,host,500,HJ212::ResponseData(object,cpStr));

            netbox.writeDataToHost(index,host,500,HJ212::ReqResult(object));

        }
        else if(1012==object.CN)
        {
            //QString cpStr;
            netbox.writeDataToHost(index,host,500,HJ212::ReqReponse(object));
            //取出
            if(object.CP17.size()>5)
            {
                Device device("","");
                HJ212::recvCN1012(object.CP17,device);
                //找出对应设备，根据协议取设置设备的时间
                station.setSystemTime(device.SystemTime);
            }

            netbox.writeDataToHost(index,host,500,HJ212::ReqResult(object));
        }
        else if(1061==object.CN)
        {
            QString cpStr;
            netbox.writeDataToHost(index,host,500,HJ212::ReqReponse(object));
            //取出
            cpStr="RtdInterval="+station.getRtdInterval();
            netbox.writeDataToHost(index,host,500,HJ212::ResponseData(object,cpStr));
            netbox.writeDataToHost(index,host,500,HJ212::ReqResult(object));
        }
        else if(1062==object.CN)
        {
            //QString cpStr;
            netbox.writeDataToHost(index,host,500,HJ212::ReqReponse(object));
            //取出
            if(object.CP17.size()>10)
            {
                station.setRtdInterval(HJ212::GetCpValueByKey(object.CP17,"RtdInterval"));
            }
            netbox.writeDataToHost(index,host,500,HJ212::ReqResult(object));
        }
        else if(1063==object.CN)
        {
            QString cpStr;
            netbox.writeDataToHost(index,host,500,HJ212::ReqReponse(object));
            //取出
            cpStr="MinInterval="+station.getMinInterval();
            netbox.writeDataToHost(index,host,500,HJ212::ResponseData(object,cpStr));
            netbox.writeDataToHost(index,host,500,HJ212::ReqResult(object));
        }
        else if(1064==object.CN)
        {
            //QString cpStr;
            netbox.writeDataToHost(index,host,500,HJ212::ReqReponse(object));
            //取出
            if(object.CP17.size()>10)
            {
                station.setMinInterval(HJ212::GetCpValueByKey(object.CP17,"MinInterval"));//textModel.setRtdInterval(HJ212::recvCN1062(object.CP17));
            }
            netbox.writeDataToHost(index,host,500,HJ212::ReqResult(object));
        }
        else if(1072==object.CN)
        {
            //QString cpStr;
            netbox.writeDataToHost(index,host,500,HJ212::ReqReponse(object));
            //取出
            if(object.CP17.size()>10)
            {
                station.setPW(HJ212::GetCpValueByKey(object.CP17,"NewPW"));//textModel.setRtdInterval(HJ212::recvCN1062(object.CP17));
            }
            netbox.writeDataToHost(index,host,500,HJ212::ReqResult(object));
        }
        else if(2011==object.CN)
        {
            //QString cpStr;
            netbox.writeDataToHost(index,host,500,HJ212::ReqReponse(object));

                station.setRealtimeReport(true);

            netbox.writeDataToHost(index,host,500,HJ212::ReqResult(object));
        }
        else if(2012==object.CN)
        {
            //QString cpStr;
            netbox.writeDataToHost(index,host,500,HJ212::ReqReponse(object));

                station.setRealtimeReport(false);

            netbox.writeDataToHost(index,host,500,HJ212::ReqResult(object));
        }
        else if(2021==object.CN)
        {
            //QString cpStr;
            netbox.writeDataToHost(index,host,500,HJ212::ReqReponse(object));

                station.setDeviceStateView(true);

            netbox.writeDataToHost(index,host,500,HJ212::ReqResult(object));
        }
        else if(2022==object.CN)
        {
            //QString cpStr;
            netbox.writeDataToHost(index,host,500,HJ212::ReqReponse(object));

                station.setDeviceStateView(false);

            netbox.writeDataToHost(index,host,500,HJ212::ReqResult(object));
        }
        else if(9013==object.CN) //上报数据的反馈处理
        {
            qDebug()<<"recvQN:"<<object.QN;
            //取反馈的平台信息
                Host* host=hostlist.getpdataByIndex(index);

            //找当前QN对应的任务并更新
                SetData::Update_ServerSendTaskState(host->id,object.QN,0);

        }
        else if(object.Flag==5)//需要应答的消息如果无法处理时返回"请求被拒绝"
        {
            netbox.writeDataToHost(index,host,500,HJ212::ReqReponse(object,2));
        }

    }
    else if(false)//返馈解包错误信息(请求命令返回)
    {
        if(error<100) netbox.writeDataToHost(index,host,500,HJ212::ReqReponse(object,error));
        //更新界面数据显示字串
        QString datastr="接收数据包错误:"+QString::number(error);
        hostlist.changeDataString(index,datastr);
    }
}




//void Station::UpDateStationInfo(QVariantMap newdata)
//{
//    station.data["MN"]=newdata["MN"].toString();
//    station.data["PW"]=newdata["PW"].toString();
//    station.data["OverTime"]=newdata["OverTime"].toString();
//    station.data["ReCount"]=newdata["ReCount"].toString();
//    station.data["MinInterval"]=newdata["MinInterval"].toString();
//    station.data["OverTime"]=newdata["OverTime"].toString();
//    station.data["RealtimeReport"]=newdata["RealtimeReport"].toBool();
//    station.data["DeviceStateView"]=newdata["DeviceStateView"].toBool();
//}

void Station::goUpDate()
{
    qDebug()<<"-goupdate-\r\n";

#if defined(Q_OS_WIN32)

    QString path=QDir::currentPath();//获取程序当前工作目录
    path.replace("/","\\");//将地址中的"/"替换为"\"，因为在Windows下使用的是"\"
    if(!QProcess::startDetached(path+"\\update.exe"))//启动主程序，主程序在其上一级目录
    {
       qDebug()<<"警告信息,启动升级程序错误!\r\n";
       return;
    }

#elif defined(Q_OS_LINUX)

    //return;

#endif

    qApp->quit();
}
